﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Spinning.Spinning.Entity.Items;
using Spinning.Spinning.Unitity;

namespace Spinning.Spinning.Entity.Battle
{
    class BattlePlayer : SingleGamePlayer
    {
        public BattlePlayer()
            : base()
        {
            _attackType = AttackType.Fire;
            IsGamePause = false;
        }

        int waterShield = 3;
        public override void OnTouchedByActor(PhysicSprite actor)
        {
            if (actor.Physic_Type == PhysicType.Player)
            {
                BattlePlayer enemy = (BattlePlayer)actor;
                if (Attack_Type == AttackType.Water && waterShield > 0)  //水屬性開盾
                {
                    MovableTextEntity blockText = new MovableTextEntity();
                    ResourceManager.ContentRoot = "";
                    blockText.fontString = "CenturyGothicBold";
                    blockText.OnLoad();
                    blockText.Origin = new Vector2(0.5f);
                    blockText.Position = new Vector2(0, -10);
                    blockText.Color = Color.Black;
                    blockText.ZIndex = Constants.debugText;
                    blockText.Static = false;
                    Attach(blockText);
                    blockText.Time = 1;
                    blockText.Path = new Vector2(0, -30);
                    blockText.Value = "Block!";
                    waterShield--;
                    if (OnShowAttackMessageEvent != null)
                    {
                        OnShowAttackMessageEvent("Block!");
                    }
                }
                else
                {
                    switch (enemy.Attack_Type)
                    {
                        case AttackType.Fire:
                            MinusLifeTime(3);
                            break;
                        case AttackType.Water:
                            MinusLifeTime(1);
                            break;
                        case AttackType.Wind:
                            MinusLifeTime(1);
                            break;
                        case AttackType.Default:
                            MinusLifeTime(1);
                            break;
                    }
                }
            }
        }

        public override void BeginContact(PhysicSprite other)
        {
            base.BeginContact(other);

            if (other.Physic_Type == PhysicType.Player)
            {
                BattlePlayer enemy = (BattlePlayer)other;
                enemy.OnTouchedByActor(this);
                //boss.MinusLifeTime(3);
                isFlash = true;
            }
        }

        int tempTimer, tempTimerFlash, flashCounter = 0;
        public bool God = false;
        protected override void UpdateSelf(GameTime gameTime)
        {
            base.UpdateSelf(gameTime);
            if (!IsGamePause)
            {
                int delta = gameTime.ElapsedGameTime.Milliseconds;
                tempTimer += delta;
                if (tempTimer / 1000 > 1)  //1秒
                {
                    tempTimer = tempTimer % 1000;
                    if (!God)
                        MinusLifeTimeSecond();
                }

                if (isFlash)
                {
                    Alpha = Math.Abs((float)Math.Sin(tempTimerFlash));
                    tempTimerFlash += delta;
                    if (tempTimerFlash / 300 > 1)
                    {
                        tempTimerFlash = tempTimerFlash % 300;
                        flashCounter++;
                        if (flashCounter == 1)
                        {
                            flashCounter = 0;
                            isFlash = false;
                            Alpha = 1;
                        }
                    }
                }
            }
        }

        protected bool isFlash = false;

    }
}
